//
//  QGRegistCodeViewController.m
//  QuanGou
//
//  Created by qqqq on 15/11/18.
//  Copyright © 2015年 DongYongFei. All rights reserved.
//

#import "QGRegistCodeViewController.h"
#import <SMS_SDK/SMSSDK.h>
#import <AVUser.h>
#import <AVFile.h>
#import "UserModel.h"

@interface QGRegistCodeViewController ()

@property (nonatomic, strong) UITextField *codeTextField;
@end

@implementation QGRegistCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"请稍等一会儿";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:(UIBarButtonItemStylePlain) target:self action:@selector(cancleClick:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:(UIBarButtonItemStylePlain) target:self action:@selector(registWithCode:)];
    self.codeTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 84, [UIScreen mainScreen].bounds.size.width - 40, 30)];
   
    self.codeTextField.borderStyle = UITextBorderStyleRoundedRect;
    self.codeTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.codeTextField becomeFirstResponder];
    [self.codeTextField setKeyboardType:UIKeyboardTypeNumberPad];
    
    [self.view addSubview:self.codeTextField];
}

- (void)cancleClick:(UIBarButtonItem *)cancleButton {
    UIAlertController *cancleController = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定取消吗？" preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [cancleController addAction:action];
    [self presentViewController:cancleController animated:YES completion:nil];

}

- (void)registWithCode:(UIBarButtonItem *)registButton {
    
    [SMSSDK commitVerificationCode:self.codeTextField.text phoneNumber:self.phoneNumber zone:@"86" result:^(NSError *error) {
        if (!error) {
            AVUser *user = [AVUser user];
            user.username = self.phoneNumber;
            user.password = self.password;
            user.mobilePhoneNumber = self.phoneNumber;
            [user setObject:@"Yes" forKey:@"mobilePhoneVerified"];
            
            AVObject *userPost = [AVObject objectWithClassName:@"UserModel"];
            
            [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                     NSLog(@"1");
                    if (self.userico != nil) {
                        NSData *imageData = UIImagePNGRepresentation(self.userico);
                        AVFile *imageFile = [AVFile fileWithName:[NSString stringWithFormat:@"%@.png", user.objectId] data:imageData];
                        [imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                            if (!error) {
                                NSLog(@"2");
                                [userPost setObject:user.objectId forKey:@"userId"];
                                [userPost setObject:self.phoneNumber forKey:@"userName"];
                                userPost[@"sex"] = @"保密";
                                [userPost setObject:imageFile forKey:@"userico"];
                                [userPost save];
                                NSLog(@"3");
                            }
                        }];
                    }
                    
#warning 直接跳转主页面

                } else {
                    NSLog(@"注册失败");
                }
            }];

        }else {
            NSLog(@"1");
//            UIAlertController *errorController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请返回重新发送" preferredStyle:(UIAlertControllerStyleAlert)];
//            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
//                [self dismissViewControllerAnimated:YES completion:nil];
//            }];
//            [errorController addAction:action];
//            [self presentViewController:errorController animated:YES completion:nil];
        }
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
